﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Security;
using System.Web.UI.WebControls;
using WebTraderLiteCL;

namespace WebTraderLite.Account
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
            if (!Page.IsPostBack)
            {
                if (Request.Cookies["username"] != null)
                {
                    TextBox textboxUserName = (TextBox)this.LoginUser.FindControl("UserName");
                    textboxUserName.Text = Server.HtmlEncode(Request.Cookies["username"].Value);

                }
            }
        }

        protected void LoginUser_LoggedIn(object sender, EventArgs e)
        {
            if (this.LoginUser.RememberMeSet == true)
            {
                HttpCookie cookie = new HttpCookie("username");
                cookie.Value = this.LoginUser.UserName.Trim();
                cookie.Expires = DateTime.Now.AddDays(1);
                Response.Cookies.Add(cookie);
                
            }
            else
            {
                if (Request.Cookies["username"] != null)
                {
                    HttpCookie myCookie = new HttpCookie("username");
                    myCookie.Expires = DateTime.Now.AddDays(-1d);
                    Response.Cookies.Add(myCookie);
                }
            }
            if (Roles.IsUserInRole(this.LoginUser.UserName, "Admin"))
            {
                Response.Redirect("~/AdminContent/AdminAccountBal.aspx");
                Response.Flush();
            }
            else if(Roles.IsUserInRole(this.LoginUser.UserName,"User"))
                Response.Redirect("~/Home.aspx");
                Response.Flush();
            }
            
      }
}
